package com.omega_r.omegarecyclerview.swipe_menu_example;

import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import ohos.multimodalinput.event.TouchEvent;

public class SwipeMenuAbility extends Ability {
    private ListContainer listContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_swipe_menu);

        final boolean[] isOtherItemTouch = {false};

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_lc_show);
        BaseProvider<Contacts> provider = new BaseProvider(getContext(), Contacts.createContactsList(20)
                , ResourceTable.Layout_item_swipe_content);
        provider.setComponentBindingListener(new BaseProvider.ComponentBindingListener() {
            @Override
            public BaseViewHolder creatViewHolder(Component item) {
                return new ContactsViewHolder(item, listContainer
                        , ResourceTable.Layout_item_left_swipe_menu
                        , ResourceTable.Layout_item_right_swipe_menu);
            }

            @Override
            public void initData(BaseViewHolder baseViewHolder, Object object, int position) {
                ((ContactsViewHolder) baseViewHolder).updateView((Contacts) object);

                baseViewHolder.setItemListener(new BaseViewHolder.ItemListener() {
                    @Override
                    public void itemTouchEvent(Component component, TouchEvent touchEvent) {
                        switch (touchEvent.getAction()) {
                            case TouchEvent.PRIMARY_POINT_DOWN:
                                isOtherItemTouch[0] = otherItemDragHandle(position);
                                break;
                        }
                    }
                });
            }
        });

        listContainer.setBoundaryThickness(2);
        listContainer.setBoundaryColor(new Color(RgbPalette.parse("#F5F5F5")));
        listContainer.setBoundarySwitch(true);

        listContainer.setItemProvider(provider);

        listContainer.setItemClickedListener((listContainer1, component, position, l) -> {
            if (!isOtherItemTouch[0])
                nowItemHandle(position);
            isOtherItemTouch[0] = false;
        });
    }

    //其它item恢复
    public boolean otherItemDragHandle(int position) {
        boolean ret = false;
        int index = listContainer.getItemPosByVisibleIndex(0);
        int numVis = listContainer.getVisibleIndexCount();
        for (int in = index; in < numVis + index; in++) {
            if (in == position) continue;
            Component tmp = listContainer.getComponentAt(in);
            Object object = tmp.getTag();
            if (object != null && object instanceof BaseViewHolder) {
                BaseViewHolder baseViewHolder = (BaseViewHolder) object;
                baseViewHolder.leftRightLayoutDragEnd(2);
                if (baseViewHolder.getItemState() != 2) {
                    ret = true;
                }
            }
        }
        return ret;
    }

    //当前item移动
    public void nowItemHandle(int position) {
        Component tmp = listContainer.getComponentAt(position);
        Object object = tmp.getTag();
        if (object != null && object instanceof BaseViewHolder) {
            BaseViewHolder baseViewHolder = (BaseViewHolder) object;
            int state = baseViewHolder.getItemState() == 1 ? 2 : (baseViewHolder.getItemState() - 1);
            baseViewHolder.leftRightLayoutDragEnd(state);
        }
    }
}
